package com.yanqu.road.dao.impl.activity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityItemRecycleDaoImpl extends TempDataObjectDao<ActivityItemRecycleData> {
    public static String getActivityItemRecycleDataSql = "select * from `t_u_item_recycle`";
    public Map<Integer, ActivityItemRecycleData> getActivityItemRecycleData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ActivityItemRecycleData> dataMap = new ConcurrentHashMap<>();

        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getActivityItemRecycleDataSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityItemRecycleData data = parse(rs);
                dataMap.put(data.getItemId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateActivityItemRecycleDataSql = "update `t_u_item_recycle` set   activityType=? , recycleType=? , recycleTime=? , paramList=? where itemId = ?";
    @Override
    protected boolean updateData(ActivityItemRecycleData activityItemRecycleData) {
        activityItemRecycleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, activityItemRecycleData.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, activityItemRecycleData.getRecycleType()));
        para.put(i++, new DbParameter(Types.BIGINT, activityItemRecycleData.getRecycleTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(activityItemRecycleData.getParamList())));
        para.put(i++, new DbParameter(Types.INTEGER, activityItemRecycleData.getItemId()));

        if (getDb().executeNoneQuery(updateActivityItemRecycleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertActivityItemRecycleDataSql = "insert into `t_u_item_recycle`(itemId,activityType,recycleType,recycleTime,paramList) values (?,?,?,?,?)";
    @Override
    protected boolean addData(ActivityItemRecycleData activityItemRecycleData) {
        activityItemRecycleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, activityItemRecycleData.getItemId()));
        para.put(i++, new DbParameter(Types.INTEGER, activityItemRecycleData.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, activityItemRecycleData.getRecycleType()));
        para.put(i++, new DbParameter(Types.BIGINT, activityItemRecycleData.getRecycleTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(activityItemRecycleData.getParamList())));
        if (getDb().executeNoneQuery(insertActivityItemRecycleDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ActivityItemRecycleData parseData(ResultSet rs) throws SQLException {
        ActivityItemRecycleData activityItemRecycleData = new ActivityItemRecycleData();
        activityItemRecycleData.setItemId(rs.getInt("itemId"));
        activityItemRecycleData.setActivityType(rs.getInt("activityType"));
        activityItemRecycleData.setRecycleType(rs.getInt("recycleType"));
        activityItemRecycleData.setRecycleTime(rs.getLong("recycleTime"));
        activityItemRecycleData.setParamList(JSON.parseObject(rs.getString("paramList"), new TypeReference<List<String>>() {}));

        return activityItemRecycleData;
    }
}
